package com.jetinno.simple.utils

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.widget.Toast
import com.jetinno.simple.SimpleApplication.Companion.sContext

/**
 * Created by zhangyuncai on 2021/1/23. OK
 */
object ToastUtil {

    private fun getContext(): Context {
        return sContext
    }

    @JvmStatic
    fun showToast(content: String?) {
        if (!content.isNullOrEmpty()) {
            showToast(content, false)
        }
    }

    @JvmStatic
    fun showToast(content: String?, isLong: Boolean) {
        if (!content.isNullOrEmpty()) {
            runOnUiThread {
                Toast.makeText(getContext(), content, if (isLong) Toast.LENGTH_LONG else Toast.LENGTH_SHORT)
            }
        }
    }

    @JvmStatic
    fun showToast(contentId: Int) {
        showToast(contentId, false)
    }

    @JvmStatic
    fun showToast(contentId: Int, isLong: Boolean) {
        showToast(getContext().resources.getString(contentId), isLong)
    }


    private fun runOnUiThread(runnable: Runnable) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            runnable.run()
        } else {
            Handler(Looper.getMainLooper()).post(runnable)
        }
    }
}